package com.ericsson.enap.modules.kpi.service;

import com.ericsson.enap.modules.kpi.entity.EnipMeasInfo;
import com.ericsson.enap.modules.kpi.entity.EnipMeasValue;
import com.ericsson.enap.modules.kpi.entity.ImsFiveKpi;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;

import java.io.File;
import java.util.*;

/**
 * 网元指标业务逻辑处理
 */
public interface EnipKpiFileProcess {
    /**
     * 网元类型
     */
    enum NetType {
        TASFIVE,PSBCCFIVE,CSCFFIVE,smf,upf,CHF,amf,pcf,hss,nrf,udm,scp,bsf,udmudr,pcfudr,
    }

    File download(ImsFiveKpi imsFiveKpi, String localDir,Logger log);

    List<File> download(ImsFiveKpi imsFiveKpi, String localDir, Logger log,Date date, int delay);

    Map<String,List<File>> download(ImsFiveKpi imsFiveKpi, String localDir, Logger log,Date date);

    List<EnipMeasInfo> analysis(List<File> files, Logger log);
    /**
     * 解析文件
     * @param file
     * @param log
     * @return
     */
    default List<EnipMeasInfo> analysis(File file, Logger log){
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(file);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element measData = doc.getRootElement().element("measData");
        String beginTime = doc.getRootElement().element("fileHeader").element("measCollec").attributeValue("beginTime");
        beginTime = beginTime.substring(0, beginTime.indexOf("+")).replace("T", " ");
        System.out.println(beginTime);
        List<?> measInfoList = measData.elements("measInfo");
        List<EnipMeasInfo> meansInfoList = new ArrayList<>();
        for (Iterator<?> it = measInfoList.iterator(); it.hasNext();) {
            EnipMeasInfo mi = new EnipMeasInfo();
            Element measInfo = (Element) it.next();
            String measInfoId = measInfo.attributeValue("measInfoId");

            Element jobNode = measInfo.element("job");
            String jobId = jobNode.attributeValue("jobId");

            List<?> measTypeList = measInfo.elements("measType");
            Map<String,String> mtMap = new HashMap<>();
            for (Iterator<?> mt = measTypeList.iterator(); mt.hasNext();) {
                Element measTypeNode = (Element) mt.next();
                String p = measTypeNode.attributeValue("p");
                String text = measTypeNode.getText();
                mtMap.put(text, p);
            }

            List<?> measValueList = measInfo.elements("measValue");
            for (Iterator<?> mv = measValueList.iterator(); mv.hasNext();) {
                EnipMeasValue measValue = new EnipMeasValue();
                Element measValueNode = (Element) mv.next();
                String measObjLdn = measValueNode.attributeValue("measObjLdn");
                List<?> rList = measValueNode.elements("r");
                Map<String,String> rMap = new HashMap<>();
                for (Iterator<?> r = rList.iterator(); r.hasNext();) {
                    Element rNode = (Element) r.next();
                    rMap.put(rNode.attributeValue("p"), rNode.getText());
                }
                measValue.setMeasObjLdn(measObjLdn);
                measValue.setRMap(rMap);
                Map<String, EnipMeasValue> mvMap = mi.getMeasValueMap();
                mvMap.put(measObjLdn, measValue);
            }

            mi.setMeasInfoId(measInfoId);
            mi.setJobId(jobId);
            mi.setMeasTypeMap(mtMap);
            mi.setBeginTime(beginTime);
            meansInfoList.add(mi);
        }
        return meansInfoList;
    }
    /**
     * 是否需要解压
     * @return
     */
    boolean needUncompress();

    /**
     * 返回网元类型
     * @return
     */
    NetType getNetType();
}
